Release 10.1A: OpenEdge Data Management:
SQL Reference
Column constraints
Specifies a constraint for a column that restricts the values that the column can store.
INSERT,UPDATE, orDELETEstatements that violate the constraint fail. The database returns a constraint violation error with an SQLCODE of -20116.Column constraints are similar to table constraints, but their definitions are associated with a single column rather than the entire table.
Syntax
CONSTRAINT constraint_nameAllows you to assign a name that you choose to the column constraint. While this specification is optional, this facilitates making changes to the column definition. If you do not specify a constraint_name, the database assigns a name. These names can be long and unwieldy, and you must query system tables to retrieve the name.
NOT NULLRestricts values in the column to values that are not null.
NOT NULL PRIMARY KEYDefines the column as the primary key for the table. There can be at most one primary key for a table. A column with the
NOT NULL PRIMARY KEYconstraint should not contain null or duplicate values.Other tables can name primary keys as foreign keys in their
REFERENCESclauses. If they do, SQL restricts operations on the table containing the primary key in the following ways:NOT NULL UNIQUEDefines the column as a unique key that cannot contain null or duplicate values. Columns with
NOT NULL UNIQUEconstraints defined for them are also called candidate keys.Other tables can name unique keys in their
REFERENCESclauses. If they do, SQL restricts operations on the table containing the unique key.REFERENCES table_name [ ( column_name ) ]Defines the column as a foreign key and specifies a matching primary or unique key in another table. The
REFERENCESclause names the matching primary or unique key.A foreign key and its matching primary or unique key specify a referential constraint. A value stored in the foreign key must either be null or be equal to some value in the matching unique or primary key.
You can omit the column_name argument if the table specified in the
REFERENCESclause has a primary key and you want the primary key to be the matching key for the constraint.CHECK ( search_condition )Specifies a column-level check constraint. SQL restricts the form of the search condition. The search condition must not:
ExamplesThe following example shows the creation of a primary key column on the supplier table:
The following example creates a
NOT NULL UNIQUEconstraint to define the columnss_noas a unique key for theemployeetable:
The following example defines
order_item.orditem_order_noas a foreign key that references the primary keyorders.order_no:
Note: The second
CREATE TABLEstatement in the previous example could have omitted the column nameorder_noin theREFERENCESclause, since it refers to the primary key of table orders.The following example creates a check constraint:
Note: If a column is defined with a
UNIQUEcolumn constraints, no error results if more than one row has aNULLvalue for the column.
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |